#######################
##
#% Module summary
##
## Main authors: 
## Progress: 
##
#######################

#!/usr/bin/env ruby

# Right now this uses xterm as the terminal
# because that works on both OSX and linux

# Launch a given amount of erlang process
# and run a test bed slave node on them
def launch_nodes(amount,master_name) 
   system("erlc *.erl")
   for i in 1..amount
      #In case the slave file isn't already compiled
      # this sucessfully calls the test module and haha funciton
      # and provides an input parameter
      #system("xterm -e 'erl -name apa"+i.to_s+" -s test haha apa1@bettan.local ' &") 
      system("xterm -bg lightblue -fg black -e 'erl -sname node"+i.to_s+" -s node start_join "+master_name+" ' &")
   end
end

# name is a string that will name the erlang process
def get_command(name)
   return "erl -name "+name+" -noshell -s test_bed_slave.erl start &"
end

# helper: determine if a string is a numeric value too
def is_numeric?(obj)
   obj.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true
end

# like a main, run launch_nodes()
# 
# the argument to pass here is
# the 
if ARGV.length != 2 || (!is_numeric? ARGV[0])
   puts "Error! Provide wanted amount of slave nodes."
else
   puts("Welcome! Let's launch "+ARGV[0].to_s+" slaves.")

   #io = IO.popen('ifconfig')
   #ifconfig = io.readlines
   #puts("ifconfig: " +ifconfig[10].to_s + ".")
   #ip = ifconfig[10].scan(/\ \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\ /)

   #puts("Your ip: " +ip.to_s + ".")
   #Here we already know it's possible to parse this 
   #input to an integer
   launch_nodes(ARGV[0].to_i,ARGV[1])
end

